package br.com.lucasddaniel.merchanmobile.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import br.com.lucasddaniel.merchanmobile.R;
import br.com.lucasddaniel.merchanmobile.adapter.TuristaSelecionadoAdapter;
import br.com.lucasddaniel.merchanmobile.bean.TuristaBean;
import br.com.lucasddaniel.merchanmobile.dao.ExcessaoBanco;
import br.com.lucasddaniel.merchanmobile.util.FilterTextWatcher;
import br.com.lucasddaniel.merchanmobile.Controller;

public class ListarClientesCompra extends Activity implements OnClickListener {

    private ArrayList<String> listaIdsPacotesComprados;
    private Integer idTuristaComprador;
    private ListView m_listview_turistas;
    private TuristaSelecionadoAdapter m_adapter_turistas;
    private List<TuristaBean> listTurista;
    private EditText m_edittext_search;
    private FilterTextWatcher filterTextWatcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listar_clientes_compra);

        listaIdsPacotesComprados = getIntent().getStringArrayListExtra("listaIdsPacotesComprados");
        try {
            listTurista = Controller.getInstance().listaTuristas(getApplicationContext());
        } catch (ExcessaoBanco e) {
            e.printStackTrace();
        }

        initComponents();
        filterTextWatcher = new FilterTextWatcher(m_adapter_turistas, m_edittext_search, R.string.activity_listar_turistas_hint);
        m_edittext_search.addTextChangedListener(filterTextWatcher);
        m_edittext_search.setInputType(EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE);
    }

    private void initComponents() {
        m_edittext_search = (EditText) findViewById(R.id_activity_listar_turistas_compra.edttext_search);
        m_listview_turistas = (ListView) findViewById(R.id_activity_listar_turistas_compra.listview);
        m_adapter_turistas = new TuristaSelecionadoAdapter(ListarClientesCompra.this, R.layout.row_turista_selecionado, listTurista);
        m_listview_turistas.setAdapter(m_adapter_turistas);
        m_listview_turistas.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int posicao, long arg3) {
                idTuristaComprador = listTurista.get(posicao).getId();

                for (TuristaBean tb : listTurista) {
                    // tb.getImg().setVisibility( View.INVISIBLE );
                    tb.setChecked(false);
                }
                m_adapter_turistas.notifyDataSetChanged();
                listTurista.get(posicao).getImg().setVisibility(View.VISIBLE);
                listTurista.get(posicao).setChecked(true);
            }
        });

        ((Button) findViewById(R.id_activity_listar_turistas_compra.btn_cadastrar)).setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id_activity_listar_turistas_compra.btn_cadastrar:
                if (idTuristaComprador == null) {
                    Toast.makeText(getApplicationContext(), "É obrigatório selecionar uma pessoa!", Toast.LENGTH_LONG).show();
                } else {
                    AlertDialog dialog3 = new AlertDialog.Builder(this).create();
                    dialog3.setMessage("É obrigatório que a impressora seja ligada nesse momento");
                    dialog3.setButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog3, int whichButton) {
                            dialog3.dismiss();
                            Intent i = new Intent(getApplicationContext(), ListarPacotesContratadosActivity.class);
                            i.putStringArrayListExtra("listaIdsPacotesComprados", listaIdsPacotesComprados);
                            i.putExtra("idTuristaComprador", idTuristaComprador);
                            //                            ActivityManager am = (ActivityManager) this.getSystemService(Activity.ACTIVITY_SERVICE);
                            //                            String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
                            //                            String className = am.getRunningTasks(1).get(0).topActivity.getClassName();
                            startActivity(i);
                        }
                    });
                    dialog3.show();
                }
                break;

            default:
                break;
        }
    }
}
